package com.haierp.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.haierp.mapper.PackageLevelMapper;
import com.haierp.model.PackageLevel;
import com.haierp.service.IPackageLevelService;
import com.haierp.util.JsonPageResult;
import com.haierp.vo.PackageLevelQueryVO;

/**
 * 
 * Title: PackageLevelServiceImpl.java
 * Description: 
 *
 * @author jc
 * Apr 8, 2017
 *
 */
@Service("packageLevelService")
public class PackageLevelServiceImpl extends SuperServiceImpl<PackageLevelMapper, PackageLevel> implements IPackageLevelService {

	@Autowired
	private PackageLevelMapper packageLevelMapper;	
	
	@Override
	public Integer queryPackageLevelsCount(PackageLevelQueryVO packageLevelQueryVO)
	{
		return packageLevelMapper.queryPackageLevelsCount(packageLevelQueryVO);
	}
	
	@Override
	public JsonPageResult<List<PackageLevel>> queryPackageLevelList(PackageLevelQueryVO packageLevelQueryVO) {
		JsonPageResult<List<PackageLevel>> packageLevelResult = new JsonPageResult<>();
		
		Integer totalCount = packageLevelMapper.queryPackageLevelsCount(packageLevelQueryVO);
		
		if ((null != totalCount) && (0L != totalCount)) {
			packageLevelResult.buildPage(totalCount, packageLevelQueryVO);
			
			List<PackageLevel> packageLevels = packageLevelMapper.queryPackageLevels(packageLevelQueryVO);
			packageLevelResult.setData(packageLevels);			
		} else {
			List<PackageLevel> packageLevels = new ArrayList<>();
			packageLevelResult.setData(packageLevels);
		}
		
		return packageLevelResult;
	}
	
	public Boolean updatePackageScaleByEnName(Long packageId, String newEnName) {
				
		PackageLevelQueryVO packageLevelQueryVO = new PackageLevelQueryVO();
		packageLevelQueryVO.setFirstStart(0);
		packageLevelQueryVO.setPackageId(packageId);;
		List<PackageLevel> list = packageLevelMapper.queryPackageLevels(packageLevelQueryVO);
		
		if (list.size() > 0) {

			for (PackageLevel packageLevel : list) {
				packageLevel.setPackageEn(newEnName);
			}
			
			 Integer count = packageLevelMapper.updateBatchById(list);
			 
			 return ((null != count) && (count > 0));
		} else {
			return true;
		}
	}
}
